<?php

 mb_internal_encoding("utf-8");
 //gc_enabled();
 require "../../document/class_corpus.php";
 require "../../document/class_document.php";
 require "../../document/class_document_xml.php";

 require "../../files/tool_files_dirs.php";

 require "../../misc/class_tag.php";
 require "../../misc/tool_color.php";

 require "../../tree/class_tree.php";
 require "../../tree/class_tree_xhtml.php";
 require "../../tree/class_structure.php";
 require "../../tree/tool_tree.php";

 require "../../array/tool_array.php";

 require "../../pear_graph/tool_pear_graph.php";

 require "../../maths/tool_maths.php";

 require "../../string/tool_string.php";
 require "../../string/class_segment.php";
 require "../../pdo/class_sql_document.php";

function is_union($o,$l,$o1,$l1){
  $e  = $o + $l - 1;
  $e1 = $o1 + $l1 - 1;
  if(($o1 < $o AND $e1 >= $o) OR ($o1 > $o AND $o1 <= $e) OR ($e1 > $e AND $o1 <= $e)) 
    return TRUE;
  else
    return FALSE;
}

function is_inside($o,$l,$o1,$l1){
  $e  = $o + $l - 1;
  $e1 = $o1 + $l1 - 1;
  if($o1 >= $o AND $e1 <= $e)
    return TRUE;
  else
    return FALSE;
}

function print_substr($array_substr){
  foreach($array_substr as $c){
    echo "    [". $c['graphie'] ."] ". $c['offset'] ." ". $c['len'] ."\n";
  }
}

function print_inside($_g,$_o,$_l,$insides){
  $res = "[$_g]\n";
  foreach($insides as $in){
    $res.="[";
    for($i=0;$i<($in['offset']-$_o);++$i){
      $res.="_";
    }
    $res.=$in['graphie'];
    for($i=0;$i<($_o+$_l - $in['offset'] - $in['len']);++$i){
      $res.="_";
    }
    $res .= "]\n";
  }
  print $res; 
}

function print_outside($_g,$_o,$_l,$outsides){
  $res = "[$_g]\n";
  $e = $_o + $_l - 1;
  foreach($outsides as $out){
    $o1 = $out['offset'];
    $l1 = $out['len'];
    $e1 = $o1 + $l1 - 1;
    $min = min($_o,$o1);
    $max = max($_e,$e1);
    $nb_inf = abs($o1 - $min);
    $nb_sup = abs($max - $e1);
//    print $out['graphie']."::".$nb_inf." ".$nb_sup."\n";
    for($i=0;$i<$nb_inf;++$i) $res .= "~";
    $res .= $out['graphie'];
    for($i=0;$i<$nb_sup;++$i) $res .= "~";
    $res .= "]\n";
  }
  print $res; 
}

$exp=new sql_experience("align_ecr_5");
$array_obj_sql=$exp->reader_experience();
$array_data=array();

$data_lg=$array_obj_sql['sql_document']->reader_all_lg();

//$str = "la commission européenne ";
//$lg = "fr";
//$str = " european commission ";
//$lg = "en";
//$str = " Euroopan komission ";
//$str = "%missio%";
//$lg = "fr";
//$str = "%commission%";
//$test = $array_obj_sql['sql_rstr']->reader_lg_graphie2array_id_rstr($lg,$str);
//$test = $array_obj_sql['sql_rstr']->reader_graphie2array_lg($str);

$id_bloc = 232;
$info = $array_obj_sql['sql_chunk']->reader_id_bloc2info_chunk($id_bloc);

print_r($info);
die();
$len = $test[0][0]["len"];
$g = $test[0][0]["graphie"];
$flag = true;
foreach($test[0][1] as $s){
  $candidats = $array_obj_sql['sql_rstr']->reader_seg2seg_inside($s['id_bloc'],$s['offset'],$len);
  $inside = $union = $outside = array();
  $o  = $s["offset"];  $l  = $len;       $e  = $o + $l - 1;
  foreach($candidats as $c){
    $o1 = $c["offset"];  $l1 = $c["len"]; $e1 = $o1 + $l1 - 1;
    $u = is_union($o,$l,$o1,$l1);
    $i = is_inside($o,$l,$o1,$l1);
    if($u) $union[]=$c;
    if($i) $inside[]=$c;
    if($u AND !$i) $outside[]=$c;
  }
//  echo "inside\n";
//  print_substr($inside);
  if($flag){
    echo "inside\n";
//    print_substr($inside);
    print_inside($g,$o,$l,$inside);
    $flag = false;
  }
  echo "outside\n";
    print_outside($g,$o,$l,$outside);

  echo "\n\n";

}

die();



//print_r($inside);
//print_r($outside);
//print_r($union);
die();

foreach($data_lg as $ligne){
  $array_id_document=$array_obj_sql['sql_document']->reader_lg2array_id_document_id_bloc($ligne['lg']);
  $correspondance_doc_bloc=array();
  $correspondance_bloc_doc=array();
  foreach($array_id_document as $ligne_doc){
    $correspondance_doc_bloc[$ligne_doc['id_document']][]=$ligne_doc['id_bloc'];
    $correspondance_bloc_doc[$ligne_doc['id_bloc']]=$ligne_doc['id_document'];
  }
  $d=$array_obj_sql['sql_rstr']->reader_all2count_bloc($ligne['lg']);
  $repartition_bloc=$repartition_global_len=array();
  $repartition_global_freq=$repartition_doc_freq=array();
  foreach($d as $l){
    $id_doc=$correspondance_bloc_doc[$l['id_bloc']];
    $repartition_bloc[$l['graphie']][$l['id_bloc']]=$l['COUNT(r.id_bloc)'];
    $repartition_global_len[$l['len']]+=$l['COUNT(r.id_bloc)'];
    $repartition_doc_len[$id_doc][$l['len']]+=$l['COUNT(r.id_bloc)'];
    $repartition_global_freq[$l['graphie']]+=$l['COUNT(r.id_bloc)'];
    $repartition_doc_freq[$id_doc][$l['graphie']]+=$l['COUNT(r.id_bloc)'];
  }

  $zipf=tool_maths::zipf($repartition_global_freq);

  $array_data[$ligne['lg']]=array(
    $correspondance_bloc_doc,
    $correspondance_doc_bloc,
    $repartition_bloc,
    $repartition_global_len,
    $repartition_doc_len,
    $repartition_global_freq,
    $repartition_doc_freq
  );

}

$motif_lg="fr";

$data_motif=$array_data[$motif_lg];
//print_r($data_motif[6]);
//die();

function rep2log_rep(&$rep){
  $rep_log=array();
  $cpt=0;
  foreach($rep as $val){
    ++$cpt;
    $lcpt="".log($cpt);
    if($val==0) $lval="-0.2";
    else $lval="".log($val);
    $rep_log[$lcpt]=$lval;
  }
  return $rep_log;
}

echo "  m  s";

function clean_rep($_rep){
  $new_rep = array();
  for($i=1;;++$i){
    if(!isset($_rep[$i])){
      return $new_rep;
    }
    $new_rep[$i]=$_rep[$i];
  }
}

foreach($array_data as $motif_lg=>$data_motif){
  $rep_global_freq=$data_motif[5];
  arsort($rep_global_freq);
  $zipf=tool_maths::own_zipf($rep_global_freq);
  $clean_rep = clean_rep($zipf[4]);
  $keys=array_keys($clean_rep);
  $min=min($keys); $max=max($keys);
  $array_k=array();
  for($min;$min<$max;++$min) $array_k[]=$min;
  $gauss=tool_maths::get_gaussienne($clean_rep,$array_k);
  echo "\n$motif_lg & $gauss[m] & $gauss[s] \\\\";
}
die();
